package com.blb.exam.controller;


import com.blb.exam.common.controller.BaseServlet;
import com.blb.exam.common.entity.PageBean;
import com.blb.exam.common.entity.QuetionSet;
import com.blb.exam.common.entity.Subject;
import com.blb.exam.common.util.SpringUtils;
import com.blb.exam.service.QuetionSetService;
import com.blb.exam.service.SubjectService;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@MultipartConfig
@WebServlet("/quetionSet.do")
public class QuetionSetServlet extends BaseServlet {

    //从spring容器中获得service对象
    private QuetionSetService quetionSetService = SpringUtils.getBean(QuetionSetService.class);

    private SubjectService subjectService = SpringUtils.getBean(SubjectService.class);

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        QuetionSet quetionSet = new QuetionSet();
        quetionSet.setTitle(req.getParameter("title"));
        quetionSet.setDescription(req.getParameter("description"));
        quetionSet.setTime(Integer.valueOf(req.getParameter("time")));
        quetionSet.setPoint(Integer.valueOf(req.getParameter("point")));
        quetionSet.setSubjectId(Integer.valueOf(req.getParameter("subjectId")));
        //添加到数据库
        int rows = quetionSetService.add(quetionSet);
        if (rows > 0) {
            //跳转到查询学生的servlet
            resp.sendRedirect("quetionSet.do?m=findPage");
        }
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        QuetionSet quetionSet = new QuetionSet();
        quetionSet.setId(Integer.valueOf(req.getParameter("id")));
        quetionSet.setTitle(req.getParameter("title"));
        quetionSet.setDescription(req.getParameter("description"));
        quetionSet.setTime(Integer.valueOf(req.getParameter("time")));
        quetionSet.setPoint(Integer.valueOf(req.getParameter("point")));
        quetionSet.setSubjectId(Integer.valueOf(req.getParameter("subjectId")));
        //添加到数据库
        int rows = quetionSetService.update(quetionSet);
        if (rows > 0) {
            //跳转到查询学生的servlet
            resp.sendRedirect("quetionSet.do?m=findPage");
        }
    }
    
    public void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得姓名参数
        String title = req.getParameter("title");
        //模糊查询
        PageBean<QuetionSet> page = quetionSetService.findPageLike(title,1);
        //保存数据，跳转
        req.setAttribute("page",page);
        req.getRequestDispatcher("/quetionSet.jsp").forward(req,resp);
    }

    public void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuId = req.getParameter("id");
        //查询学生
        QuetionSet quetionSet = quetionSetService.findById(Long.valueOf(stuId));
        //将数据传到更新页面
        req.setAttribute("quetionSet",quetionSet);
        req.getRequestDispatcher("/updateQuetionSet.jsp").forward(req,resp);
    }

    //填充科目到session，用于显示下拉菜单
    private void addSubjectsToSession(HttpSession session){
        List<Subject> subjects = subjectService.selectAll();
        Object sub = session.getAttribute("subjects");
        if(sub == null){
            session.setAttribute("subjects",subjects);
        }
    }

    //分页查询
    public void findPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String current = req.getParameter("current");
        String title = req.getParameter("title");
        title = title == null ? "" : title;
        int cur = 1;
        if(current != null){
            cur = Integer.parseInt(current);
        }
        PageBean<QuetionSet> page = quetionSetService.findPageLike(title, cur);
        //将数据传到更新页面
        req.setAttribute("page",page);
        req.getRequestDispatcher("/quetionSet.jsp").forward(req,resp);
        //填充科目到session，用于显示下拉菜单
        addSubjectsToSession(req.getSession());
    }


    public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuId = req.getParameter("id");
        //添加到数据库
        int rows = quetionSetService.deleteById(Integer.parseInt(stuId));
        if(rows > 0){
            //跳转到查询学生的servlet
            resp.sendRedirect("quetionSet.do?m=findPage");
        }
    }
}
